package com.corpgovernment.organization.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.corpgovernment.api.organization.model.user.nonemployee.*;
import com.corpgovernment.common.base.CommonException;
import com.corpgovernment.common.base.Page;
import com.corpgovernment.common.utils.EncryptUtils;
import com.corpgovernment.common.utils.PageUtils;
import com.corpgovernment.organization.entity.db.MbOrgNonEmployeeInfo;
import com.corpgovernment.organization.mapper.MbOrgNonEmployeeMapper;
import com.corpgovernment.organization.service.IOrganizationNonEmployeeService;
import com.corpgovernment.organization.service.OrganizationBaseService;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.ctrip.corp.obt.shard.context.PageContext;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * 员工用户信息service实现
 *
 * @author wangyujue
 */
@Service
@Slf4j
public class OrganizationNonEmployeeServiceImpl extends OrganizationBaseService implements IOrganizationNonEmployeeService {

    @Autowired
    private MbOrgNonEmployeeMapper mbOrgNonEmployeeMapper;

    @Override
    public CreateOrUpdateOrgNonEmployeeResponse createOrUpdate(CreateOrUpdateOrgNonEmployeeRequest request) {
        // 创建员工用户
        if (request.getId() == null) {
            validate(request);
            MbOrgNonEmployeeInfo mbOrgNonEmployeeInfo = new MbOrgNonEmployeeInfo();
            BeanUtils.copyProperties(request, mbOrgNonEmployeeInfo);
            int result = mbOrgNonEmployeeMapper.insertSelective(mbOrgNonEmployeeInfo);
//            insertOperateLog(mbOrgNonEmployeeInfo);
            CreateOrUpdateOrgNonEmployeeResponse createOrUpdateOrgNonEmployeeResponse = new CreateOrUpdateOrgNonEmployeeResponse();
            createOrUpdateOrgNonEmployeeResponse.setId(mbOrgNonEmployeeInfo.getId());
            return createOrUpdateOrgNonEmployeeResponse;
        }
        // 更新员工用户
        if (request.getId() != null) {
            MbOrgNonEmployeeInfo mbOrgNonEmployeeInfo = new MbOrgNonEmployeeInfo();
            BeanUtils.copyProperties(request, mbOrgNonEmployeeInfo);
            int result = mbOrgNonEmployeeMapper.updateByPrimaryKeySelective(mbOrgNonEmployeeInfo);
            try {
                MbOrgNonEmployeeInfo oldValue = mbOrgNonEmployeeMapper.selectByPrimaryKey(mbOrgNonEmployeeInfo.getId());
                updateOperateLog(oldValue, mbOrgNonEmployeeInfo);
            } catch (Exception e) {
                log.error("更新操作获取oldValue失败，OrganizationNonEmployeeServiceImpl.createOrUpdate");
            }
        }
        return new CreateOrUpdateOrgNonEmployeeResponse();
    }

    @Override
    public Page page(SearchOrgNonEmployeePageRequest request) {
        Example example = new Example(MbOrgNonEmployeeInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getOrgId())) {
            criteria.andEqualTo("orgId", request.getOrgId());
        }
        if (StringUtils.isNotBlank(request.getName())) {
            criteria.andLike("name", "%" + request.getName() + "%");
        }
        example.selectProperties("uid", "name");
        // 分页
        PageContext.startPage(request.getPageNum(), request.getPageSize());
        List<MbOrgNonEmployeeInfo> list = mbOrgNonEmployeeMapper.selectByExample(example);
        return PageUtils.optPageBy(list, OrgNonEmployeeVo.class);
    }

    @Override
    public GetOrgNonEmployeeResponse find(GetOrgNonEmployeeRequest request) {
        MbOrgNonEmployeeInfo mbOrgNonEmployeeInfo = new MbOrgNonEmployeeInfo();
        BeanUtils.copyProperties(request, mbOrgNonEmployeeInfo);
        MbOrgNonEmployeeInfo result = mbOrgNonEmployeeMapper.selectOne(mbOrgNonEmployeeInfo);
        if (result != null) {
            GetOrgNonEmployeeResponse getOrgNonEmployeeResponse = new GetOrgNonEmployeeResponse();
            BeanUtils.copyProperties(result, getOrgNonEmployeeResponse);
            return getOrgNonEmployeeResponse;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        deleteOperateLog(String.valueOf(id));
        return mbOrgNonEmployeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean batchSave(List<MbOrgNonEmployeeInfo> mbOrgNonEmployeeInfoList) {
        int i = mbOrgNonEmployeeMapper.batchSave(mbOrgNonEmployeeInfoList);
//        mbOrgNonEmployeeInfoList.forEach(this::insertOperateLog);
        return i > 0;
    }

    @Override
    public boolean save(MbOrgNonEmployeeInfo mbOrgNonEmployeeInfo) {
        int i = mbOrgNonEmployeeMapper.insertSelective(mbOrgNonEmployeeInfo);
//        insertOperateLog(mbOrgNonEmployeeInfo);
        return i > 0;
    }

    @Override
    public List<MbOrgNonEmployeeInfo> listByIds(List<Long> ids) {
        return mbOrgNonEmployeeMapper.listByIds(ids);
    }

    @Override
    public boolean update(MbOrgNonEmployeeInfo mbOrgNonEmployeeInfo) {
        Example example = new Example(MbOrgNonEmployeeInfo.class);
        example.createCriteria().andEqualTo("id", mbOrgNonEmployeeInfo.getId());
        try {
            MbOrgNonEmployeeInfo oldValue = mbOrgNonEmployeeMapper.selectOneByExample(example);
//            updateOperateLog(oldValue, mbOrgNonEmployeeInfo);
        } catch (Exception e) {
            log.error("更新操作获取oldValue失败，OrganizationNonEmployeeServiceImpl.update");
		}
		int i = mbOrgNonEmployeeMapper.updateByExampleSelective(mbOrgNonEmployeeInfo, example);
		return i > 0;
	}

	@Override
	public boolean deleteByUid(String uid) {
		return mbOrgNonEmployeeMapper.deleteByUid(uid) > 0;
	}

	@Override
	public List<MbOrgNonEmployeeInfo> findByName(String name) {
		MbOrgNonEmployeeInfo selectParam = new MbOrgNonEmployeeInfo();
		selectParam.setName(name);
		return mbOrgNonEmployeeMapper.select(selectParam);
	}

    @Override
    public MbOrgNonEmployeeInfo findByUidAndName(String uid, String name) {
        Example example = new Example(MbOrgNonEmployeeInfo.class);
        example.createCriteria().andEqualTo("employeeUid", uid).andEqualTo("name", EncryptUtils.sm4Encrypt(name));
        List<MbOrgNonEmployeeInfo> mbOrgNonEmployeeInfos = mbOrgNonEmployeeMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(mbOrgNonEmployeeInfos)){
            return null;
        }
        return mbOrgNonEmployeeInfos.get(0);
    }


    private void validate(OrgNonEmployeeVo vo) {
		if (vo == null) {
			throw new CommonException(10001, "参数不能为空");
		}
		if (StringUtils.isBlank(vo.getName())) {
			throw new CommonException(10003, "员工用户姓名不能为空");
		}
		if (vo.getValid() == null) {
			throw new CommonException(10004, "员工用户状态不能为空");
		}
	}
}
